<?php
require_once(ROOT_ADMIN_MODELS.DS.'Models_User.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Group.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Base.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_News.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Log.php'); 
require_once(ROOT_LIBRARY_UTILITY.DS.'utility.php');
require_once(ROOT_ADMIN_CONTROLLERS.DS.'BaseController.php');

require_once(ROOT_ADMIN_OBJECT.DS.'Objs_News.php');

require_once(ROOT_ADMIN_FORMS.DS.'Forms_News.php');

class Admin_NewsController extends BaseController {
	
	public function preDispatch()
	{
		parent::preDispatch();	
		if(!$this->hasPrivilege())
			$this->_redirect ("/admin/error/permission");
	}
		
	public function indexAction()
	{
		try 
		{	
			$this->view ->headTitle("UIT-News Supporter");		
			$md = new Models_News();
			$typenews= ""; $forum= ""; $status= "all"; $author= "";$from= "";$to = "";
			if($this->_request->isPost())
			{
				$typenews= $this->_request->getParam('typenews');
				$forum= $this->_request->getParam('forum'); 
				$status= $this->_request->getParam('status'); 
				$author= $this->_request->getParam('author');
				$from= $this->_request->getParam('from');
				$to = $this->_request->getParam('to');
			}
			$this->view->arrNews = $md->listNews($typenews, $forum, $status, $author,$from,$to);
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		} 	
	}
	
	public function viewAction()
	{
		$id = $this->_request->getParam('id');
		$md = new Models_News();
		$this->view->news = $md->NewsByid($id);
	}
	
	public function activeAction()
	{
		try{
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNorender();
			if($this->_request->isPost())
			{
				$id = $this->_request->getParam('id');
				$status = $this->_request->getParam("active");
				
				$md = new Models_News();
				$md->active($id, $status);
			}
			echo ("1");
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function deleteAction()
	{
		try{
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNorender();
			if($this->_request->isPost())
			{
				$id = $this->_request->getParam('id');
		
				$md = new Models_News();
				$md->delete($id);
			}
			echo ("1");
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	public function listnewsAction()
	{
		try{
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNorender();
			
			$md= new Models_News();
			$arrtin = $md->listNews("","","1","","","");
			echo(json_encode($arrtin));
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
}